<?php

namespace zhtfrontend\controllers;

use common\models\datas\Balance;
use common\models\datas\Admissionfee;
use common\models\datas\BalanceRelation;
use Yii;
use common\components\helpers\Fun;
use zhtfrontend\models\datas\Mechanic;
use yii\helpers\Json;
use yii\data\Pagination;
use yii\web\Response;
use yii\helpers\ArrayHelper;
use common\components\extenders\AjaxObject;

class AdmissionController extends \zhtfrontend\components\bases\BaseController
{

    public function actionIndex()
    {
        $param['searchData'] = \Yii::$app->request->get();
        $pageSize = isset($param['pageSize']) ? $param['pageSize'] : 12;
        $data = Admissionfee::find()->where(['site_id' => Fun::getSiteId()]);
        if (!empty($param['searchData'])) {
            $search_data = array_filter($param['searchData']);

            if (isset($search_data['mechanism_id']) && $search_data['mechanism_id'] > 0) {
                $data->andWhere(['mechanic_id' => $search_data['mechanism_id']]);
            }
            if (isset($search_data['searchWay']) && $search_data['searchWay'] > 0) {
                if ($search_data['searchWay'] == 2) {
                    $data->andWhere(['between', 'created_at', strtotime(date('Y-m-d', strtotime($search_data['searchDate']))), strtotime(date('Y-m-d', strtotime("+1 day", strtotime($search_data['searchDate']))))]);
                } else {
                    $data->andWhere(['between', 'created_at', strtotime(date('Y-m', strtotime($search_data['searchDate']))), strtotime(date('Y-m', strtotime("next month", strtotime($search_data['searchDate']))))]);
                }
            }
            if (isset($search_data['state']) && $search_data['state'] > 0) {
                if ($search_data['state'] == 1) {
                    $data->andWhere(['status' => 1]);
                } else {
                    $data->andWhere(['status' => 0]);
                }
            }
        }

        $model = $data->orderBy('id DESC');
        $pages = new Pagination(['totalCount' => $model->count(), 'pageSize' => $pageSize]);
        isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
        $model = $data->offset($pages->offset)->limit($pages->limit)->all();
        $array = Fun::asArray($model, ['mechanic_name', 'goods_name', 'student_name', 'created_at', 'status', 'checkbox']);
        $modelMechanic = Mechanic::find()->all();
        $listData = ArrayHelper::map($modelMechanic, 'id', 'name');

        return $this->render('index', ['listData' => $listData, 'data' => $array, 'pages' => $pages]);
    }

    /**
     *
     */
    public function actionBatchupdate()
    {
        if (\Yii::$app->request->isAjax) {
            $post = \Yii::$app->request->post();
            if (!empty($post['ids'])) {
                $ids = $post['ids'];
                $idsArr = explode(',', $ids);
                $items = Admissionfee::findAll($idsArr);
                $arr = [];
                foreach ($items as $val) {
                    $result = BalanceRelation::find()->where(['relation_id' => $val->id, 'type' => 0])->one();
                    if (empty($result)) {
                        $arr[] = $val->mechanic_id;
                    }
                }
                if ($arr) {
                    $newarr = array_unique($arr);
                    foreach ($newarr as $m_id) {
                        $price = 0;
                        foreach ($items as $item) {
                            $result = BalanceRelation::find()->where(['relation_id' => $item->id, 'type' => 0])->one();
                            if ($item->mechanic_id == $m_id && empty($result)) {
                                $price += $item->admissionfee;
                            }
                            $admissionfee = Admissionfee::findOne(['id' => $item->id]);
                            $admissionfee->status = 1;
                            $admissionfee->save(false);
                        }
                        $model = new Balance();
                        $model->title = '';
                        $model->mechanic_id = $m_id;
                        $model->price = $price;
                        $model->user_id = \Yii::$app->user->id;
                        $model->created_at = time();
                        $model->updated_at = 0;
                        $model->voucher = '';
                        $model->type = 0;
                        $model->site_id = Fun::getSiteId();
                        $model->save(false);
                        if ($model->balance_id > 0) {
                            foreach ($items as $goods) {
                                $result = BalanceRelation::find()->where(['relation_id' => $goods->id, 'type' => 0])->one();
                                if ($goods->mechanic_id == $m_id && empty($result)) {
                                    $srModel = new BalanceRelation();
                                    $srModel->relation_id = $goods->id;
                                    $srModel->balance_id = $model->balance_id;
                                    $srModel->type = 0;
                                    $srModel->save(false);
                                }
                            }
                        }
                    }
                    return 1;
                }
                return 2;
            } else {
                return 3;
            }
        }

    }


}
